package com.yinwq.shorturl.controller;

import com.yinwq.shorturl.service.ShortService;
import com.yinwq.shorturl.utils.Result;
import com.yinwq.shorturl.utils.ResultUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: yinwenqiang@qccr.com
 * @Date: Created in 2019/8/18 11:54 AM
 */

@RestController
public class ShortUrlController {

    @Resource
    private ShortService shortService;

    @RequestMapping("/getShortUrl")
    public Result generateShorUrl(String longUrl){

        String shortUrl = shortService.generateShorUrl(longUrl);

        return ResultUtil.success(shortUrl);
    }

    @RequestMapping("/{shortUrlCode}")
    public void redirectLongUrl(@PathVariable String shortUrlCode, HttpServletResponse response) throws IOException {
        String longUrl = shortService.queryLongUrl(shortUrlCode);
        response.sendRedirect(longUrl);
    }


}
